#!/usr/bin/env python
#coding:utf-8

import os
import sys
import wx
from about import wpAbout

class MainWindow(wx.Frame):
    """ We simply derive a new class of Frame. """
    def __init__(self, parent, id, title):
        self.width = 800
        self.height = 600
        wx.Frame.__init__(self, parent, id, title, size=(self.width, self.height))
        self.control = wx.TextCtrl(self, 1, pos=(2, 2),
                                   size=(self.width-10, self.height-50),
                                   style=wx.TE_MULTILINE)
        font = wx.Font(12, wx.DECORATIVE, wx.NORMAL, wx.NORMAL)
        self.control.SetFont(font)
        
        self.Bind(wx.EVT_MOTION, self.OnMotion)
        self.statusbar = self.CreateStatusBar() #创建状态栏
        self.statusbar.SetFieldsCount(2)
        self.statusbar.SetStatusWidths([-1, -2])
        self.createMenuBar() #创建菜单
        self.file = ''

        self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        self.buttons = []

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.control, 1, wx.EXPAND)
        self.sizer.Add(self.sizer2, 0, wx.EXPAND)
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)
        self.Show(True)

    def OnMotion(self, event):
        self.statusbar.SetStatusText("Line Count: %s" % len(self.lines),1)
        event.Skip()

    def menuData(self):
        '''创建菜单项'''
        return (("&File",
                 ("新建文件(&N)", "新建一个Blog文件", self.OnNew),
                 ("&Open", "打开一个已有的文件", self.OnOpen),
                 ("&Save", "保存文件", self.OnSave),
                 ("Save as...", '文件另存为...', self.OnSaveAs),
                 ("", "", ""),
                 ("&Quit", "退出wxBlog管理器", self.OnCloseWindow)),
                ("&Edit",
                 ("Undo", 'Undo', self.OnUndo),
                 ("Redo", 'Redo', self.OnRedo),
                 ("&Copy", "Copy", self.OnCopy),
                 ("C&ut", "Cut", self.OnCut),
                 ("&Paste", "Paste", self.OnPaste),
                 ("", "", ""),
                 ("&Options", "DisplayOptions", self.OnOptions)),
                ("&Help",
                 ("Help", "hele for this software", self.OnHelp),
                 ("About", "about for this software", self.OnAbout))
                )
    def createMenuBar(self):
        menuBar = wx.MenuBar()
        for eachMenuData in self.menuData():
            menuLabel = eachMenuData[0]
            menuItems = eachMenuData[1:]
            menuBar.Append(self.createMenu(menuItems), menuLabel)
        self.SetMenuBar(menuBar)

    def createMenu(self, menuData):
        menu = wx.Menu()
        for eachLabel, eachStatus, eachHandler in menuData:
            if not eachLabel:
                menu.AppendSeparator()
                continue
            menuItem = menu.Append(-1, eachLabel, eachStatus)
            self.Bind(wx.EVT_MENU, eachHandler, menuItem)
        return menu

    def OnNew(self, event):pass
    
    def OnOpen(self, event):
        '''打开文件'''
        self.dirname = ""
        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)

        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            f = open(os.path.join(self.dirname, self.filename), 'r')
            self.control.SetValue(f.read())
            f.close()
        dlg.Destroy()
        
    def OnSave(self, event):
        '''保存文件'''

        if self.file == '':
            dlg = wx.FileDialog(None, 'wxPython wp', style=wx.SAVE)
            if dlg.ShowModal() == wx.ID_OK:
                self.file = dlg.GetPath()
                self.control.SaveFile(self.file)
            dlg.Destroy()
        else:
            self.control.SaveFile(self.file)

    def OnSaveAs(self, event):
        '''文件另存为'''
        dlg = wx.FileDialog(None, 'wxPython wp', style=wx.SAVE)
        if dlg.ShowModal() == wx.ID_OK:
            self.file = dlg.GetPath()
            self.control.SaveFile(self.file)
        dlg.Destroy()
        
    def OnCloseWindow(self, event):
        '''关闭窗口'''
        self.Close(True)

    def OnUndo(self, event):
        '''撤销'''
        self.control.Undo()

    def OnRedo(self, event):
        '''重做'''
        self.control.Redo()
        
    def OnCopy(self, event):
        '''复制'''
        self.control.Copy()
        
    def OnCut(self, event):
        '''剪切'''
        self.control.Cut()
        
    def OnPaste(self, event):
        '''粘贴'''
        self.control.Paste()
        
    def OnOptions(self, event):pass
    
    def OnHelp(self, event):pass
    
    def OnAbout(self, event):
        '''关于程序'''
        dlg = wpAbout(self)
        dlg.ShowModal()
        dlg.Destroy()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MainWindow(None, -1, "wxBlog")
    app.MainLoop()
    
    
